package com.siwei.mes.service.experiment.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.siwei.mes.common.query.PageQuery;
import com.siwei.mes.entity.experiment.Experiment;
import com.siwei.mes.entity.experiment.ExperimentCheckTypeRecord;
import com.siwei.mes.mapper.experiment.ExperimentCheckTypeRecordMapper;
import com.siwei.mes.service.experiment.ExperimentCheckTypeRecordService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

import static com.siwei.mes.common.exception.handler.MessageHandler.assertTrue;

/**
 * 原材料实验台账检查类型记录表(ExperimentCheckTypeRecord)表服务实现类
 *
 * @author makejava
 * @since 2024-08-02 22:35:36
 */
@Service
public class ExperimentCheckTypeRecordServiceImpl implements ExperimentCheckTypeRecordService {
    @Resource
    private ExperimentCheckTypeRecordMapper experimentCheckTypeRecordMapper;

    @Override
    public Long create(ExperimentCheckTypeRecord entity) {
        experimentCheckTypeRecordMapper.insertSelective(entity);
        return entity.getId();
    }

    @Override
    public void update(ExperimentCheckTypeRecord entity) {
        ExperimentCheckTypeRecord experimentCheckTypeRecord = experimentCheckTypeRecordMapper.selectByPrimaryKey(entity.getId());
        assertTrue(experimentCheckTypeRecord == null, "原材料实验台账检查类型记录表信息不存在");
        experimentCheckTypeRecordMapper.updateByPrimaryKeySelective(entity);
    }

    @Override
    public void delete(Long id) {
        ExperimentCheckTypeRecord experimentCheckTypeRecord = experimentCheckTypeRecordMapper.selectByPrimaryKey(id);
        assertTrue(experimentCheckTypeRecord == null, "原材料实验台账检查类型记录表信息不存在");
        experimentCheckTypeRecordMapper.deleteByPrimaryKey(id);
    }

    @Override
    public PageInfo<ExperimentCheckTypeRecord> getPage(PageQuery<ExperimentCheckTypeRecord, ExperimentCheckTypeRecord> query) {
        ExperimentCheckTypeRecord searchParam = query.getParams();
        return PageHelper.startPage(query.getPageNum(), query.getPageSize())
                .doSelectPageInfo(() -> experimentCheckTypeRecordMapper.getList(searchParam));
    }

    @Override
    public ExperimentCheckTypeRecord loadById(Long id) {
        return experimentCheckTypeRecordMapper.loadById(id);
    }

    @Override
    public List<ExperimentCheckTypeRecord> getAll(ExperimentCheckTypeRecord entity) {
        return experimentCheckTypeRecordMapper.getList(entity);
    }

    @Override
    public void saveExperimentCheckTypeRecord(Experiment entity) {
        ExperimentCheckTypeRecord experimentCheckTypeRecord = new ExperimentCheckTypeRecord();
        experimentCheckTypeRecord.setExperimentId(entity.getId());
        experimentCheckTypeRecord.setExperimentType(entity.getExperimentType());
        experimentCheckTypeRecord.setCheckType(Long.valueOf(entity.getCheckType()));
        experimentCheckTypeRecord.setCheckTimeType(Long.valueOf(entity.getCreateCheckType()));
        experimentCheckTypeRecord.setCreateTime(new Date());
        experimentCheckTypeRecord.setMaterialsId(Long.valueOf(entity.getMaterialsId()));
        experimentCheckTypeRecordMapper.insert(experimentCheckTypeRecord);
    }
}
